/*
 * Created by JFormDesigner on Wed Jun 07 10:42:01 CST 2023
 */

package gui.admin;

import dao.SupplierDao;
import dao.UserDao;
import entity.Supplier;
import entity.User;
import gui.admin.tablemodel.SupplierTableModel;
import gui.admin.tablemodel.UserTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class ManageSupplierFrame extends JFrame {
    public ManageSupplierFrame() throws Exception {
        initComponents();
    }

    private void initComponents() throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        supplierId = new JTextField();
        supplierName = new JTextField();
        address = new JTextField();
        contactNumber = new JTextField();
        updateBtn = new JButton();
        deleteBtn = new JButton();
        cancelBtn = new JButton();

        //======== this ========
        setTitle("\u4f9b\u8d27\u5546\u4fe1\u606f\u7ba1\u7406\u9875\u9762");
        setPreferredSize(new Dimension(960, 535));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new SupplierTableModel());
            table1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(new Rectangle(new Point(455, 30), scrollPane1.getPreferredSize()));

        //---- label1 ----
        label1.setText("\u4f9b\u8d27\u5546\u7f16\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(20, 35, 100, 40);

        //---- label2 ----
        label2.setText("\u4f9b\u8d27\u5546\u540d\u79f0");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(20, 110, 100, 40);

        //---- label3 ----
        label3.setText("\u4f9b\u8d27\u5546\u5730\u5740");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(20, 185, 100, 40);

        //---- label4 ----
        label4.setText("\u8054\u7cfb\u7535\u8bdd");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label4);
        label4.setBounds(20, 260, 100, 40);
        contentPane.add(supplierId);
        supplierId.setBounds(130, 35, 260, 45);
        contentPane.add(supplierName);
        supplierName.setBounds(130, 110, 260, 45);
        contentPane.add(address);
        address.setBounds(130, 185, 260, 45);
        contentPane.add(contactNumber);
        contactNumber.setBounds(130, 260, 260, 45);

        //---- updateBtn ----
        updateBtn.setText("\u4fee\u6539");
        updateBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(updateBtn);
        updateBtn.setBounds(65, 380, 95, 45);

        //给修改按钮添加点击事件
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //获取文本输入框内容
                String supplierIdTemp = supplierId.getText();
                String supplierNameTemp = supplierName.getText();
                String contactNumberTemp = contactNumber.getText();
                String addressTemp = address.getText();
                //创建Supplier对象
                Supplier supplier = new Supplier(supplierIdTemp,supplierNameTemp,contactNumberTemp,addressTemp);
                SupplierDao supplierDao = new SupplierDao();
                boolean result = supplierDao.updateSupplier(supplier);
                if(supplierId.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"请先选择要修改的供货商");
                }else{
                    if(result){
                        JOptionPane.showMessageDialog(null,"修改供货商成功");
                        SupplierTableModel supplierTableModel = null;
                        try {
                            supplierTableModel = new SupplierTableModel();
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                        //刷新表格
                        table1.setModel(supplierTableModel);
                    }else{
                        JOptionPane.showMessageDialog(null,"修改用户失败");
                    }
                }
            }
        });

        //---- deleteBtn ----
        deleteBtn.setText("\u5220\u9664");
        deleteBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(deleteBtn);
        deleteBtn.setBounds(195, 380, 95, 45);

        //给删除按钮添加点击事件
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int res = JOptionPane.showConfirmDialog(null,"你确定要删除该供货商吗？","删除确认",JOptionPane.YES_NO_OPTION);
                if(res == 0){
                    if (supplierId.getText().equals("")){
                        JOptionPane.showMessageDialog(null,"请先选择要删除的供货商！");
                    }else {
                        Supplier supplier = new Supplier();
                        supplier.setSupplierId(supplierId.getText());
                        SupplierDao supplierDao = new SupplierDao();
                        try {
                            String msgSting = supplierDao.deleteSupplier(supplier);
                            JOptionPane.showMessageDialog(null,msgSting);
                            SupplierTableModel supplierTableModel = new SupplierTableModel();
                            table1.setModel(supplierTableModel);
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            }
        });

        //---- cancelBtn ----
        cancelBtn.setText("\u53d6\u6d88");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(325, 380, 95, 45);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //给表格添加鼠标点击事件
        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //super.mouseClicked(e);
                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                //获取点击行供货商信息
                String supplierIdTemp = (String) table1.getValueAt(row,0);
                String supplierNameTemp = (String) table1.getValueAt(row,1);
                String contactNumberTemp = (String) table1.getValueAt(row,2);
                String addressTemp = (String) table1.getValueAt(row,3);
                //设置输入框的值
                supplierId.setText(supplierIdTemp);
                supplierName.setText(supplierNameTemp);
                contactNumber.setText(contactNumberTemp);
                address.setText(addressTemp);
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField supplierId;
    private JTextField supplierName;
    private JTextField address;
    private JTextField contactNumber;
    private JButton updateBtn;
    private JButton deleteBtn;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        ManageSupplierFrame manageSupplierFrame = new ManageSupplierFrame();
    }
}
